package com.shuma.shopmanage.shopmodule.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mysql.fabric.xmlrpc.base.Data;
import com.shuma.shopmanage.common.exception.ShopManageException;
import com.shuma.shopmanage.common.result.Result;
import com.shuma.shopmanage.common.result.ResultCodeEnum;
import com.shuma.shopmanage.model.shop.model.Operation;
import com.shuma.shopmanage.model.shop.model.OperationVo;
import com.shuma.shopmanage.shopmodule.service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class OperationController {

    @Autowired
    OperationService operationService;

    @GetMapping("/getOperation")
    public Result<List<OperationVo>> getOperation(Long goodsId) {
        List<Operation> operationList = null;
        ArrayList<OperationVo> operationVos;
        try {
            operationList = operationService.query().eq("goodsId", goodsId).list();
            operationVos = new ArrayList<>();
            for (Operation operation : operationList) {
                OperationVo operationVo = BeanUtil.copyProperties(operation, OperationVo.class);
                operationVo.setTypeName(OperationVo.status.get(operation.getType()).toString());
                operationVos.add(operationVo);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopManageException(ResultCodeEnum.FAIL);
        }
        return Result.ok(operationVos);

    }
}
